package com.macrosoftinc.feedreaderxtreme.adapter;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.macrosoft.frpd.bo.FeedItem;
import com.macrosoft.frpd.utils.Logger;
import com.macrosoft.frpd.utils.Logger.DebugLevel;
import com.macrosoftinc.feedreaderxtreme.fragments.CategoriesDetailFragment;

public class CustomFragmentPagerAdapter extends FragmentPagerAdapter {

	String fragmentName;

	List<FeedItem> catFeedItems;

	private List<CategoriesDetailFragment> fragments;

	public CustomFragmentPagerAdapter(FragmentManager fm, List<FeedItem> catFeedItems) {
		super(fm);
		// this.fragments = fragments;
		this.catFeedItems = catFeedItems;
		if (fragments != null)
			fragments.clear();
		// setFragments(null);
//		Log.i(this.getClass().getSimpleName(), "" + getFragments().size());
		if (getFragments() != null) {
			Log.i(this.getClass().getSimpleName(), "" + getFragments().size());
			for (Fragment fragment : getFragments()) {
				Log.i(this.getClass().getSimpleName(), "fragment.getTag()===" + fragment.getTag());
			}
		}
		fragments = new ArrayList<CategoriesDetailFragment>(catFeedItems.size());
	}

	/**
	 * Very Important method
	 */
	// @Override
	// public void destroyItem(ViewGroup container, int position, Object object) {
	// // TODO Auto-generated method stub
	// super.destroyItem(container, position, object);
	// Logger.log(DebugLevel.INFO, "/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*onDestroy==*CustomFragmentPagerAdapter destroyItem position==" + position);
	// }

	@Override
	public Fragment getItem(int position) {
		// return fragments.get(position);
		CategoriesDetailFragment categoriesDetailFragment = CategoriesDetailFragment.newInstance(catFeedItems.get(position).getTitle(), catFeedItems.get(position).getURL(),
				position, catFeedItems.get(position).getProperties());
		fragments.add(categoriesDetailFragment);
//		Log.i(this.getClass().getSimpleName(), "setCategoriesFragmentsList------adapterOftorrentsOfCategory.getFragments().size()---fragments" + fragments.size());
		return categoriesDetailFragment;
	}

	@Override
	public int getItemPosition(Object object) {
		return PagerAdapter.POSITION_NONE;
	}

	@Override
	public int getCount() {
		return catFeedItems.size();
	}

	/**
	 * @return the fragments
	 */
	public List<CategoriesDetailFragment> getFragments() {
		return fragments;
	}

	/**
	 * @param fragments
	 *            the fragments to set
	 */
	public void setFragments(List<CategoriesDetailFragment> fragments) {
		this.fragments = fragments;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {

		// TODO Auto-generated method stub
	}

	@Override
	public CharSequence getPageTitle(int position) {
//		Log.i(this.getClass().getSimpleName(), "getPageTitle called" + position);
		return "" + catFeedItems.get(position).getTitle().toUpperCase();
	}
}
